using System.Xml;

namespace ServiceModel.Syndication.LEAP2A {
    /// <summary>
    /// Represents an country in a spatial address
    /// </summary>
    public class LeapSpatialCountry {
        /// <summary>
        /// Gets or sets the country code.
        /// </summary>
        /// <value>The country code.</value>
        public string CountryCode {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a human readable version of the country name.
        /// </summary>
        /// <value>The value.</value>
        public string Value {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapSpatialCountry"/> class.
        /// </summary>
        public LeapSpatialCountry() {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapSpatialCountry"/> class.
        /// </summary>
        /// <param name="code">The country code.</param>
        /// <param name="name">The country name.</param>
        public LeapSpatialCountry(string code, string name) {
            this.CountryCode = code;
            this.Value = name;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapSpatialCountry"/> class.
        /// </summary>
        /// <param name="name">The country name.</param>
        public LeapSpatialCountry(string name) {
            this.Value = name;
        }

        /// <summary>
        /// Writes to a LEAP2A feed.
        /// </summary>
        /// <param name="writer">The writer.</param>
        public void WriteTo(XmlWriter writer, LeapFeed feed) {
            if (!string.IsNullOrEmpty(Value) || !string.IsNullOrEmpty(CountryCode)) {
                writer.WriteStartElement(
                    feed.LeapPredicatesNamespacePrefix,
                    "country",
                    feed.LeapPredicatesNamespace);

                if (!string.IsNullOrEmpty(CountryCode)) {
                    writer.WriteElementString(
                         feed.LeapPredicatesNamespacePrefix,
                        "countrycode",
                        feed.LeapPredicatesNamespace,
                        CountryCode);
                }

                if (!string.IsNullOrEmpty(Value)) {
                    writer.WriteValue(Value);
                }

                writer.WriteEndElement();
            }
        }
    }
}
